package com.work.keep.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.datas.DynamicDetailsData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/19.
 */
public class DynamicDetailsAdapter extends BaseAdapter {
    private Context context;
    private List<DynamicDetailsData.DataEntity.CommentsListEntity> commentsList;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;


    public DynamicDetailsAdapter(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        commentsList = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    public void addData(List<DynamicDetailsData.DataEntity.CommentsListEntity> commentsList) {
        this.commentsList = commentsList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return commentsList.size();
    }

    @Override
    public Object getItem(int position) {
        return commentsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DynamicDetailsViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.dynamic_details_comments_item, parent, false);
            holder = new DynamicDetailsViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (DynamicDetailsViewHolder) convertView.getTag();
        }
        getAvaterView(holder, position);
        holder.commentsItemName.setText(commentsList.get(position).getAuthor().getUsername());
        holder.commentsItemDate.setText(commentsList.get(position).getCreated());
        holder.commentsItemContent.setText(commentsList.get(position).getContent());
        return convertView;
    }

    private void getAvaterView(DynamicDetailsViewHolder holder, int position) {
        holder.commentsItemAvater.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.commentsItemAvater.setErrorImageResId(R.mipmap.ic_launcher);
        holder.commentsItemAvater.setImageUrl(commentsList.get(position).getAuthor().getAvatar(), imageLoader);
    }

    class DynamicDetailsViewHolder {
        NetworkImageView commentsItemAvater;
        TextView commentsItemName, commentsItemDate, commentsItemContent;

        public DynamicDetailsViewHolder(View view) {
            commentsItemAvater = (NetworkImageView) view.findViewById(R.id.commentsItemAvater);
            commentsItemName = (TextView) view.findViewById(R.id.commentsItemName);
            commentsItemDate = (TextView) view.findViewById(R.id.commentsItemDate);
            commentsItemContent = (TextView) view.findViewById(R.id.commentsItemContent);
        }
    }
}
